@extends('layouts.back_stage')
@section('content')
    <style>
        .towhite:hover,.towhite:focus{
            color:white;
        }
        .write_phone{
            border:none;
            outline: none;
            width:100px;
            margin-left:5px;
            background-color:rgba(0,0,0,0);
        }
        .table th {
            text-align: center;
        }
    </style>
    <div class="container">
        <div style="padding:20px 0;"><h3>邮费录入</h3></div>
        <div class="container_box">
            <div class="chartshead cost_entry_head">
                <form action="/finance/postageEntry" method="get" role="form">
                <p style="line-height:30px;font-size:13px;">所属公司：</p>
                <select class="form-control getType getCompany" style="width:130px;height:30px;padding:0 12px;" name="company_id" >
                    @if(count($req_info['company_id']==0))
                        <option value="">请选择</option>
                    @endif

                    @foreach($companies as $k=>$v)
                        <option value="{{$k}}"
                    @if($req_info['company_id'] == $k)
                            selected
                        @endif
                        >{{$v}}</option>
                    @endforeach
                </select>
                <p style="line-height:30px;font-size:13px;">客户名称：</p>
                <input type="text" style="width: 150px;height:30px;" class="form-control" autocomplete="off" value="{{$req_info['username']}}" name="username" placeholder="客户姓名" />
                <p style="line-height:30px;font-size:13px;">手机号：</p>
                <input type="tel" style="width: 150px;height:30px;" class="form-control" autocomplete="off" value="{{$req_info['user_tel']}}" name="user_tel" placeholder="输入手机号" />
                <p style="line-height:30px;font-size:13px;">时间：</p>
                <select class="form-control getType getYear" style="width:130px;height:30px;padding:0 12px;" name="year" >
                    @if(isset($req_info['year']) && $req_info['year'] !='')
                        <option value="{{$req_info['year']}}">{{$req_info['year']}}</option>
                    @else
                        <option value="<?php echo date('Y')?>" selected><?php echo date('Y')?></option>
                    @endif
                    <option value="2016">2016</option>
                    <option value="2017">2017</option>
                    <option value="2018">2018</option>
                </select>
                <p style="line-height:30px;font-size:13px;margin-left:1%;margin-right:1%;">年</p>
                <select class="form-control getType getMonth" style="width:130px;height:30px;padding:0 12px;" name="month" >
                    @if(isset($req_info['month']) && $req_info['month'] !='')
                        <option value="{{$req_info['month']}}">{{$req_info['month']}}</option>
                    @else
                        <option value="<?php echo date('m')?>" selected><?php echo date('m')?></option>
                    @endif
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                </select>
                <p style="line-height:30px;font-size:13px;margin-left:1%;margin-right:1%;">月</p>
                <input type="submit" class="btn serach_cost btn-info" value="搜索">
                </form>
            </div>
            <table  border="0" cellspacing="0" style="position: relative;text-align:center" class="table table-striped table-hover">
                <thead>
                <tr>
                    <th>客户姓名</th>
                    <th>客户手机号</th>
                    <th>所属公司</th>
                    <th>当月邮费（共¥{{$all_postages->all_postages or 0}}元）</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach($infos as $k => $v)
                <tr>
                    <td>{{$v->username}}</td>
                    <td>{{$v->user_tel}}</td>
                    <td>{{$companies[$v->company_id]}}</td>
                    <td class="user-postage">¥<input type="number" class="write_phone" max="9999" min="0" placeholder="请输入邮费" value="{{$v->postage}}" abc="{{$v->postage}}" readonly></td>
                    <td id="{{$v->id}}"><button class="btn btn-info save-postage">录入</button></td>
                </tr>
                    @endforeach
                </tbody>
            </table>
            <span style="float: right">{{$infos->appends(['user_tel'=>$req_info['user_tel'],'year'=>$req_info['year'],'month'=>$req_info['month'],'company_id'=>$req_info['company_id'],'username'=>$req_info['username']])->links()}}</span>
        </div>
    </div>
    <script type="text/javascript">
        $(document).on('click','.save-postage',function() {
            var that_ = $(this);
            var id = $(this).parent().attr('id');
            var postage = $(this).parent().siblings('.user-postage').children('.write_phone').val();
            var getYear = $('.getYear').val();
            var getMonth = $('.getMonth').val();
            var status;
            if($(this).parent().siblings().siblings('.user-postage').children('.write_phone').attr('abc') == ""){
                status = 0;
            }else{
                status = 1;
            }
            if(postage < 0){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text('请输入正确的邮费');
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            if($(this).text() == "录入"){
                $(this).text("确认");
                that_.parent().siblings('.user-postage').children('.write_phone').removeAttr('readonly');
            }else{
                $.ajax({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                    },
                    data:{
                        type:status,
                        user_id:id,
                        postage:postage,
                        year:getYear,
                        month:getMonth
                    },
                    type:"post",
                    url:"/finance/postage",
                    dataType:'json',
                    success:function (res) {
                        if(res.success == true){
                            that_.text('录入');
                            that_.parent().siblings('.user-postage').children('.write_phone').attr('readonly','readonly');
                        }else{
                            $(".faceImg").attr("src","/img/weep.png");
                            $(".oHandle").text(res.msg);
                            $(".oSuccess").show(0).delay(1000).hide(0);
                        }
                    }
                })
            }
        })
    </script>
@endsection
